<template>
  <div>
    <h2>随机点名</h2>
    <div class="box">
      <span>名字是：</span>
      <div class="qs">{{ name }}</div>
    </div>
    <div class="btns">
      <button class="start" @click="start" :disabled="falg">开始</button>
      <button class="end" @click="end" :disabled="Control">结束</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "这里显示名字",
      general: ["赵云", "马超", "黄忠", "张飞", "关羽"],
      timer: "",
      random: "",
      falg: false,
      Control: false,
    };
  },
  methods: {
    start() {
      this.falg = !this.falg;
      if (this.general.length === 1) {
        this.falg = true
        this.Control=true
        return alert("没人了，还点个鸡儿");
      }
      this.timer = setInterval(() => {
        this.name = this.general[parseInt(Math.random() * this.general.length)];
      }, 10);
    },
    end() {
      this.falg = !this.falg;
      clearInterval(this.timer);
      this.random = parseInt(Math.random() * this.general.length);
      this.name = this.general[this.random];
      this.general.splice(this.random, 1);
    },
  },
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

h2 {
  text-align: center;
}

.box {
  width: 600px;
  margin: 50px auto;
  display: flex;
  font-size: 25px;
  line-height: 40px;
}

.qs {
  width: 450px;
  height: 40px;
  color: red;
}

.btns {
  text-align: center;
}

.btns button {
  width: 120px;
  height: 35px;
  margin: 0 50px;
}
</style>